package com.dandelion.manager.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.dandelion.commons.spring.SpringContextHolder;
import com.dandelion.commons.web.model.AjaxResponse;
import com.dandelion.commons.web.model.DataGrid;
import com.dandelion.manager.system.domain.Menu;
import com.dandelion.manager.system.repository.MenuMapper;
import com.dandelion.manager.system.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private MenuMapper mapper ;

	@CacheEvict(value="SystemMenuCache", allEntries=true)
	@Override
	public AjaxResponse add(Menu record) {
		if(this.mapper.existsFileldValue(null, "text", record.getText()) > 0) 
			return AjaxResponse.failMsg("该名称已存在！") ;
		if(this.mapper.existsFileldValue(null, "identity", record.getIdentity()) > 0) 
			return AjaxResponse.failMsg("该权限标示已存在！") ;
		
		record.setSorting(this.mapper.nextSort());
		this.mapper.insertSelective(record) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@CacheEvict(value="SystemMenuCache", allEntries=true)
	@Override
	public AjaxResponse delete(long id) {
		this.mapper.deleteByPrimaryKey(id) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}
	
	@CacheEvict(value="SystemMenuCache", allEntries=true)
	@Override
	public AjaxResponse deleteBatch(Long[] ids) {
		this.mapper.deleteByPrimaryKeys(ids) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}


	@CacheEvict(value="SystemMenuCache", allEntries=true)
	@Override
	public AjaxResponse update(Menu record) {
		if(this.mapper.existsFileldValue(record.getId(), "text", record.getText()) > 0) 
			return AjaxResponse.failMsg("该名称已存在！") ;
		if(this.mapper.existsFileldValue(record.getId(), "identity", record.getIdentity()) > 0) 
			return AjaxResponse.failMsg("该权限标示已存在！") ;
		
		this.mapper.updateByPrimaryKeySelective(record) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Cacheable(value="SystemMenuCache" ,key="#root.targetClass +\".\"+ #root.methodName + (\"(\"+#id+\")\")")
	@Override
	public Menu get(long id) {
		return this.mapper.selectByPrimaryKey(id) ;
	}

	@Cacheable(value="SystemMenuCache" ,key="#root.targetClass +\".\"+ #root.methodName + (\"()\")")
	@Override
	public List<Menu> getAllList(Menu record) {
		return this.mapper.selectAllList(record);
	}
	
	@Cacheable(value="SystemMenuCache" ,key="#root.targetClass +\".\"+ #root.methodName + (\"()\")")
	@Override
	public DataGrid<Menu> datagrid(Menu conditions) {
		DataGrid<Menu> dg = new DataGrid<Menu>() ;
		
		Map<String, Object> conditionsMap = new HashMap<String, Object>() ;
		conditionsMap.put("record", conditions) ;
		conditionsMap.put("offset", dg.getOffset()) ;
		conditionsMap.put("rows", dg.getPageSize()) ;
		
		List<Menu> list = this.mapper.selectAllListPage(conditionsMap) ;
		int count = this.mapper.getCount(conditionsMap) ;
		dg.setRows(list) ;
		dg.setTotal(count);
		
		return dg;
	}
	
}
